//
//  MZAniBar.m
//  MZAnimationDemo
//
//  Created by Chris Yang on 2016/11/24.
//  Copyright © 2016年 Chris Yang. All rights reserved.
//

#import "MZDemoSupprot.h"

#import "UIView+MZLayout.h"

#import "MZAniBar.h"

@implementation MZAniBar

- (id)init {
    
    self = [super init];
    
    if (self) {
        [self configSelf];
        [self allocSubviews];
        [self configSubviews];
        [self positionSubviews];
    }
    
    return self;
}

- (void)configSelf {
    
}

- (void)allocSubviews {
    _scrollView = [[UIScrollView alloc] init];
}

- (void)configSubviews {
    
    for (int i = 0; i < _itemButtonArray.count; i++) {
        UIButton *tmpButton = _itemButtonArray[i];
        [tmpButton setTag:i];
        [tmpButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self addSubview:_scrollView];
    for (UIButton *tmpButton in _itemButtonArray) {
        [_scrollView addSubview:tmpButton];
    }
    
    UIButton *tmpButton = _itemButtonArray[0];
    [tmpButton setSelected:YES];
}

- (void)positionSubviews {
    
    CGRect tmpRect = CGRectZero;
    
    tmpRect = self.bounds;
    [_scrollView setFrame:tmpRect];
    
    CGFloat tmpSpace = 15;
    /* Item Buttons */
    tmpRect = [self mzCenterRectInSubviewWithWidth:55 height:55 toLeft:tmpSpace];
    for (UIButton *tmpButton in _itemButtonArray) {
        [tmpButton setFrame:tmpRect];
        [tmpButton.layer setCornerRadius:tmpRect.size.width / 2];
        tmpRect.origin.x += (tmpRect.size.width + tmpSpace);
    }
    
    /* Content Size */
    tmpRect.size.height = _scrollView.bounds.size.height;
    tmpRect.size.width = MAX(_scrollView.bounds.size.width + 1,
                             tmpRect.origin.x);
    [_scrollView setContentSize:tmpRect.size];
}

- (void)layoutSubviews {
    [self positionSubviews];
}

#pragma mark - Target-Action Pair

- (void)buttonClicked:(UIButton *)button {
    
    if ([_delegate respondsToSelector:@selector(aniBar:itemButtonClickedAtIndex:)]) {
        [_delegate aniBar:self itemButtonClickedAtIndex:button.tag];
    }
}

#pragma mark - MISC

#pragma mark - User Interface

- (void)resetWithTitleArray:(NSArray<NSString *> *)titleArray {
    
    for (UIButton *tmpButton in _itemButtonArray) {
        [tmpButton removeFromSuperview];
    }
    
    NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < titleArray.count; i++) {
        
        UIButton *tmpButton = [MZDemoSupprot mzDemoButton];
        [tmpButton setTitle:titleArray[i] forState:UIControlStateNormal];
        [tmpArray addObject:tmpButton];
    }
    _itemButtonArray = [NSArray arrayWithArray:tmpArray];
    
    [self configSubviews];
    [self positionSubviews];
}

- (void)resetWithTitleTagDic:(NSDictionary<NSString *, NSNumber *> *)titleTagDic {

}

- (void)setAllSelected:(BOOL)selected {
    
    for (UIButton *tmpButton in _itemButtonArray) {
        [tmpButton setSelected:selected];
    }
}

- (void)setSelected:(BOOL)selected atIndex:(NSInteger)index {
 
    if (index < 0 || index >= _itemButtonArray.count) {
        return;
    }
    
    UIButton *tmpButton = _itemButtonArray[index];
    [tmpButton setSelected:selected];
}

@end
